home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / drive / Timer.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.2 KB  |  178 lines

  1. /*
  2.  * Copyright 1992-1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "Timer.h"
  18.  
  19. const int RUNNING = 1;
  20. const int STOPPED = 0;
  21.  
  22. void WatchTime::set(SbTime time)
  23. {
  24.     long sec, usec;
  25.     
  26.     time.getValue(sec, usec);
  27.  
  28.     _hours = sec/3600;
  29.     _minutes = sec/60;
  30.     _seconds = sec%60;
  31.     _tenths = usec/100000;
  32. }
  33.  
  34. WatchTime operator +(const WatchTime &t0, const WatchTime &t1)
  35. {
  36.     WatchTime result(
  37.         t0._hours + t1._hours, 
  38.         t0._minutes + t1._minutes, 
  39.         t0._seconds + t1._seconds, 
  40.         t0._tenths + t1._tenths);
  41.  
  42.     if (result._tenths >= 10)
  43.     {
  44.         result._tenths -= 10;
  45.         result._seconds++;
  46.     }
  47.  
  48.     if (result._seconds >= 60)
  49.     {
  50.         result._seconds -= 60;
  51.         result._minutes++;
  52.     }
  53.  
  54.     if (result._minutes >= 60)
  55.     {
  56.         result._minutes -= 60;
  57.         result._hours++;
  58.     }
  59.  
  60.     return result;
  61. }
  62.  
  63.  
  64.  
  65. void Timer::reset()
  66. {
  67.     _this_lap.reset();
  68.     _last_lap.reset();
  69.     _best_lap.reset(); // XXX should read from file
  70.  
  71.     _state = STOPPED;
  72. }
  73.  
  74. void Timer::update()
  75. {
  76.     if (_state == RUNNING)
  77.     {
  78.         SbTime now = SbTime::getTimeOfDay();
  79.         WatchTime delta(now - _start);
  80.         _this_lap = _offset + delta;
  81.     }
  82. }
  83.  
  84.  
  85. void Timer::lap()
  86. {
  87.     static Boolean best_lap_set = FALSE;
  88.  
  89.     if ((_this_lap < _best_lap) || (! best_lap_set))
  90.     {
  91.         _best_lap = _this_lap;
  92.         best_lap_set = TRUE;
  93.     }
  94.  
  95.     _last_lap = _this_lap;
  96.     _this_lap.set(0,0,0,0);
  97.     
  98.     start();
  99. }
  100.  
  101.  
  102. void Timer::start()
  103. {
  104.     _offset = _this_lap;
  105.     _start = SbTime::getTimeOfDay();
  106.     _state = RUNNING;
  107. }
  108.  
  109.  
  110. void Timer::stop()
  111. {
  112.     _state = STOPPED;
  113. }
  114.  
  115.  
  116. // sets string to be in the form 0:00:00.0
  117. void WatchTime::setString(char buf[10]) const
  118. {
  119.     sprintf(buf,"%1d:%2d:%2d.%1d",_hours,_minutes,_seconds,_tenths);
  120.  
  121.     // stick in leading zeros
  122.     if (_minutes < 10)
  123.         buf[2] = '0';
  124.     if (_seconds < 10)
  125.         buf[5] = '0';
  126.  
  127.     buf[9] = NULL;
  128. }
  129.  
  130. Boolean Timer::get_this_lap(char *(&result)) const
  131. {
  132.     static WatchTime last(-1,-1,-1,-1);
  133.     static char buf[10]; // 0:00:00.0
  134.     Boolean changed;
  135.     
  136.     if (changed = (last != _this_lap))
  137.     {
  138.         _this_lap.setString(buf);
  139.         last = _this_lap;
  140.     }
  141.     
  142.     result = &(buf[0]);
  143.     return changed;
  144. }
  145.  
  146. Boolean Timer::get_last_lap(char *(&result)) const
  147. {
  148.     static WatchTime last(-1,-1,-1,-1);
  149.     static char buf[10]; // 0:00:00.0
  150.     Boolean changed;
  151.     
  152.     if (changed = (last != _last_lap))
  153.     {
  154.         _last_lap.setString(buf);
  155.         last = _last_lap;
  156.     }
  157.     
  158.     result = &(buf[0]);
  159.     return changed;
  160. }
  161.  
  162. Boolean Timer::get_best_lap(char *(&result)) const
  163. {
  164.     static WatchTime last(-1,-1,-1,-1);
  165.     static char buf[10]; // 0:00:00.0
  166.     Boolean changed;
  167.     
  168.     if (changed = (last != _best_lap))
  169.     {
  170.         _best_lap.setString(buf);
  171.         last = _best_lap;
  172.     }
  173.     
  174.     result = &(buf[0]);
  175.     return changed;
  176. }
  177.  
  178.